package registnet.base;

import java.util.List;
import java.util.ListIterator;
import java.util.Vector;

/**
 * This class provides default implementations for the <code>ICurriculum</code> interface.
 */
public abstract class AbstractCurriculum implements ICurriculum {
	
	/**
	 * The code for the curriculum. 
	 */
	protected String curriculumCode;
	
	/**
	 * The list of courses associated with the curriculum.
	 */
	protected List<ICourse> courses;
	
	@Override
	public void addCourse(ICourse course) {
		courses.add(course);
	}
	
	@Override
	public boolean contains(ICourse course) {
		ListIterator<ICourse> li = courses.listIterator();
		while(li.hasNext())
			if(course.equals(li.next()))
				return true;
		return false;
	}
	
	@Override
	public boolean contains(String courseCode) {
		ListIterator<ICourse> li = courses.listIterator();
		while(li.hasNext())
			if(courseCode.equals(li.next().getCourseCode()))
				return true;
		return false;
	}
	
	@Override
	public List<ICourse> getAllCourses() {
		return courses;
	}
	
	@Override
	public ICourse getCourse(String courseCode) {
		ListIterator<ICourse> li = courses.listIterator();
		while(li.hasNext()){
			ICourse tmpCourse = li.next();
			if(courseCode.equals(tmpCourse.getCourseCode()))
				return tmpCourse;
		}
		return null;
	}
	
	@Override
	public List<ICourse> getCourses(int year, int semester) {
		List<ICourse> retval = new Vector<ICourse>();
		ListIterator<ICourse> li = courses.listIterator();
		while(li.hasNext()){
			ICourse tmpCourse = li.next();
			if(year==tmpCourse.getYear() && semester==tmpCourse.getSemester())
				retval.add(tmpCourse);
		}
		return retval;
	}
	
	@Override
	public List<ICourse> getCourses(int year) {
		List<ICourse> retval = new Vector<ICourse>();
		ListIterator<ICourse> li = courses.listIterator();
		while(li.hasNext()){
			ICourse tmpCourse = li.next();
			if(year==tmpCourse.getYear())
				retval.add(tmpCourse);
		}
		return retval;
	}
	
	@Override
	public String getCurriculumCode() {
		return curriculumCode;
	}
	
	@Override
	public int getTotalUnits() {
		int retval = 0;
		ListIterator<ICourse> li = courses.listIterator();
		while(li.hasNext()){
			ICourse tmpCourse = li.next();
			if(tmpCourse.getCourseCode().contains("PE ") || tmpCourse.getCourseCode().contains("NSTP "))
				continue;
			retval = retval + tmpCourse.getUnit();
		}
		return retval;
	}
	
	@Override
	public int getTotalUnits(int year) {
		int retval = 0;
		ListIterator<ICourse> li = courses.listIterator();
		while(li.hasNext()){
			ICourse tmpCourse = li.next();
			if(tmpCourse.getCourseCode().contains("PE ") || tmpCourse.getCourseCode().contains("NSTP "))
				continue;
			if(tmpCourse.getYear() == year)
				retval = retval + tmpCourse.getUnit();
		}
		return retval;
	}
	
	@Override
	public void setCurriculumCode(String curriculumCode) {
		this.curriculumCode = curriculumCode;
		
	}

	@Override
	public int getUnits(int year, int semester) {
		ListIterator<ICourse> li = this.courses.listIterator();
		int totalUnits = 0;
		while(li.hasNext()){
			ICourse course = li.next();
			if(course.getCourseCode().contains("PE ") || course.getCourseCode().contains("LTS ") || course.getCourseCode().contains("CWTS ") || course.getCourseCode().contains("ROTC ") || course.getCourseCode().contains("NSTP "))
				continue;
			if(course.getYear() == year && course.getSemester() == semester){
				totalUnits += course.getUnit();
			}
		}
		return totalUnits;
	}
	
	
	
}
